package com.baosight.sczx.zm.cg.service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiColumn;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.core.threadlocal.UserSession;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.StaticBlocks;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.cs.Engineering;
import com.baosight.sczx.common.zm.cInterface.cs.WorkSchedule;
import com.baosight.sczx.zm.cg.domain.ZMCGTSPGDispatch;
import com.baosight.sczx.zm.cg.domain.ZMCGTsPg;

/**
 * 竖井提升和排矸情况
 * <p>Title: ServiceZMCG73.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 严洪浩
 * @version 1.0
 * <pre>Histroy:
 *          2011-6-27 上午08:01:23  严洪浩  Create
 *</pre>
 */
public class ServiceZMCG73 extends ServiceEPBase {
	/**
	 * 初始进入方法
	 * <p>Function: initLoad</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCG73</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2013-6-25 上午14:37:20 王开兵
	 * </p>
	 *
	 */
	@Override
	public EiInfo initLoad(EiInfo inInfo) 
	{
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的
		EiInfo outInfo = this.query(inInfo);
		return outInfo;
	}
	
	/**
	 * 查询方法
	 * <p>Function: query</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCG73</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2013-6-25 上午14:47:20 王开兵
	 * </p>
	 *
	 */
	@Override
	public EiInfo query(EiInfo inInfo) 
	{
		StaticTools.setQueryParameters(inInfo, "fdMiningId", UserSession.getInSessionProperty("miningCode"), true);
		EiInfo outInfo = super.query(inInfo, "ZMCGTsPg.query", new ZMCGTsPg());
		outInfo.addBlock(StaticBlocks.getStauts4());
		outInfo.addBlock(Engineering.getEngineeringForJJ("cmes.cp.engineering", "1", "", "1", false,UserSession.getInSessionProperty("miningCode").toString()));//工程名称
		outInfo.addBlock(getMines());
		outInfo.addBlock(WorkSchedule.getWorkScheduleBlock("cmes.cp.workNum", false));//班次
//		outInfo.addBlock(Department.getInstance().getConstructionUnit("cmes.cp.department", "orgCode", "orgName", false)); //队别
//		outInfo.addBlock(Department.getInstance().getMiningDepts("cmes.cp.department", "orgCode", "orgName", false,UserSession.getInSessionProperty("miningCode").toString()));
		outInfo.set("fdCreatorId", UserSession.getUserId());//登录人的默认值
		outInfo.set("fdCreator", UserSession.getInSessionProperty("UserCname"));
		outInfo.set("fdCreateDepartmentId", UserSession.getInSessionProperty("orgCode"));//单位的默认值		
		outInfo.set("fdCreateDepartment", UserSession.getInSessionProperty("orgName"));//单位的默认值
		outInfo.set("fdMiningId", UserSession.getInSessionProperty("miningCode"));//矿ID的默认值
		outInfo.set("fdMiningName", UserSession.getInSessionProperty("miningName"));//矿名称的默认值
		
		return outInfo;
	}

	public EiBlock getMines() {
		EiBlock block = new EiBlock("cmes.ch.mine");

		EiColumn EiColumn = new EiColumn("display");
		EiColumn.setType("C");
		block.addMeta(EiColumn);

		EiColumn = new EiColumn("value");
		EiColumn.setType("C");
		block.addMeta(EiColumn);

		List<Map> _list = new ArrayList<Map>();
		Map<String, String> _map = new HashMap<String, String>();
		_map.put("value", "");
		_map.put("display", "");
		_list.add(_map);

		List<Map> _listData = new ArrayList<Map>();
		try {
			_listData = dao.query("ZMCGTsPg.queryTreeChild", null);
		} catch (Exception e) {}
		
		for(int i = 0; i < _listData.size(); i++) {
			_list.add(_listData.get(i)); 
		} 
		block.setRows(_list);
		return block;
	}

	/**
	 * 新增方法
	 * <p>Function: insert</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCBCoalseam</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2013-6-25上午11:34:17 王开兵
	 * </p>
	 *
	 */
	@Override
	public EiInfo insert(EiInfo inInfo) 
	{
		EiInfo out=new EiInfo(); 
		ZMCGTsPg _dom = new ZMCGTsPg();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			//校验是否重复
			Map<String, String> params = new HashMap<String, String>();
			params.put("fdKeyId", _dom.getFdKeyId());
			params.put("fdWorknumId", _dom.getFdWorknumId());
			params.put("fdVirtualDate", _dom.getFdVirtualDate());
			params.put("fdMiningName", _dom.getFdMiningName());
			String fdMiningName = _dom.getFdMiningName();
//        boolean validate = validate(inInfo, _dom, "ZMCGTsPg.validate", params);
        boolean validate = this.validate(inInfo, _dom, params);
        if (validate) {
			out = this.query(inInfo);
			return inInfo;
		}else {
			_dom.setFdReportDepartment(UserSession.getInSessionProperty("orgName").toString());// 汇报部门
			_dom.setFdMiningId(UserSession.getInSessionProperty("miningCode").toString());	//矿井id
			_dom.setFdMiningName(UserSession.getInSessionProperty("miningName").toString());
			_dom.setFdKeyId(TablePk.getInstance().getNextValue("T_CG_TSPG"));
		    this.dao.insert("ZMCGTsPg.insert", _dom);
		   }		
		}
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out; 		
				
	}

	/**
	 * 修改方法
	 * <p>Function: update</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCBCoalseam</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2013-6-24上午13:34:17 王开兵
	 * </p>
	 *
	 */
	@Override
	public EiInfo update(EiInfo inInfo) 
	{
		EiInfo out=new EiInfo();
		ZMCGTsPg _dom = new ZMCGTsPg();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			//校验是否重复
			Map<String, String> params = new HashMap<String, String>();
			params.put("fdKeyId", _dom.getFdKeyId());

		    this.dao.update("ZMCGTsPg.update", _dom);
		   
		}
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out; 		
		
		
		
}
	
	/**
	 * 删除方法
	 * <p>Function: delete</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCBCoalseam</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2013-6-26上午13:34:17 王开兵
	 * </p>
	 *
	 */
	@Override
	public EiInfo delete(EiInfo inInfo) 
	{

		EiInfo outInfo = null;
		
		List<String> _list = new ArrayList<String>();
		Map<String,List<String>> params = new HashMap<String,List<String>>();

		for(int i = 0; i < inInfo.getBlock(EiConstant.resultBlock).getRowCount(); i++)
		{

			EiInfo _temp = super.delete(inInfo, "ZMCGTsPg.delete");
	        outInfo = this.query(inInfo);
	        outInfo.setMsg(_temp.getMsg());
	        outInfo.setDetailMsg(_temp.getDetailMsg());
		}
		
		return outInfo;
	}
	
	/**
	 * 上报
	 * <p>Function: report</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-7-6下午04:07:27 张志芳
	 * </p>
	 *
	 */
	public EiInfo report(EiInfo inInfo)
	{
		EiInfo out=new EiInfo(); 
		ZMCGTsPg _dom = new ZMCGTsPg();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			
			Map<String, String> params = new HashMap<String, String>();
			params.put("fdKeyId", _dom.getFdKeyId());

			String fdKeyId = _dom.getFdKeyId();
			String fdVirtualDate = _dom.getFdVirtualDate();
			String fdWorknumId = _dom.getFdWorknumId();
			BigDecimal fdPgValue = _dom.getFdPgValue();
			BigDecimal fdGanziValue = _dom.getFdGanziValue();
			BigDecimal fdClValue = _dom.getFdClValue();
			this.dao.update("ZMCGTsPg.report",_dom);
			ZMCGTSPGDispatch bean = new ZMCGTSPGDispatch();	
			String  fdStatus = "未发布";
			bean.setFdMiningId(UserSession.getInSessionProperty("miningCode").toString());	//矿井id
			bean.setFdMiningName(UserSession.getInSessionProperty("miningName").toString());// 矿井名称
			bean.setFdReportDepartment(UserSession.getInSessionProperty("orgName").toString());// 汇报部门
			bean.setFdStatus(fdStatus);
			bean.setFdVirtualDate(fdVirtualDate);
			bean.setFdWorknumId(fdWorknumId);
			bean.setFdPgValue(fdPgValue);
			bean.setFdGanziValue(fdGanziValue);
			bean.setFdClValue(fdClValue);
			bean.setFdKeyId(fdKeyId);
			this.dao.insert("ZMCGTSPGDispatch.insert",bean);
		}	
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out;   
	
	}
	
	
	/**
	 * 验证上报是否重复
	 * <p>Function: validate</p>
	 * <p>Description: </p>
	 * <p>return value:boolean</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-9-6上午09:04:59 严洪浩
	 * </p>
	 *
	 */
//	@SuppressWarnings({ "rawtypes", "unchecked" })
//	private boolean validate(EiInfo inInfo, ZMCGTsPg _dom, String sqlName, Map<String, String> params) {
//		if(params == null) {
//			params = new HashMap<String, String>();
//		}
//		String fdWorknumId = _dom.getFdWorknumId();
//		String fdVirtualDate = _dom.getFdVirtualDate();
//		String fdMiningName =_dom.getFdMiningName();
//		params.put("fdWorknumId", fdWorknumId);
//		params.put("fdVirtualDate", fdVirtualDate);
//		params.put("fdMiningName", fdMiningName);
//		List result = dao.query("ZMCGTsPg.validate", params);
//		if(result != null && result.size() > 0) {
//			inInfo.setStatus(-1);
//			String msg = "每矿每天只能填写一份！";
//			inInfo.setMsg(msg);
//			return true;
//		}
//		return false;
//	}
	@SuppressWarnings("rawtypes")
	private boolean validate(EiInfo inInfo, ZMCGTsPg _dom, Map<String, String> params) {
		if(params == null) {
			params = new HashMap<String, String>();
		}
		
		String fdWorknumId = _dom.getFdWorknumId();
		String fdVirtualDate = _dom.getFdVirtualDate();
		String fdMiningName =_dom.getFdMiningName();
		
		params.put("fdWorknumId", fdWorknumId);
		params.put("fdVirtualDate", fdVirtualDate);
		params.put("fdMiningName", fdMiningName);
		
		List result = dao.query("ZMCGTsPg.validate", params);
		if(result != null && result.size() > 0) {
			inInfo.setStatus(-1);
			ZMCGTsPg _temp = (ZMCGTsPg)result.get(0);
		
			StringBuffer msg = new StringBuffer("提交失败，");

			if(_temp.getFdWorknumId() != null && _temp.getFdWorknumId().equals(fdWorknumId)) {
				msg.append(" 班次 ");
			}
			if(_temp.getFdVirtualDate() != null && _temp.getFdVirtualDate().equals(fdVirtualDate)) {
				msg.append(" 日期 ");
			}

		msg.append("重复！");
		
		inInfo.setMsg(msg.toString());
			return true;
		}
		return false;
	}
	
}
	
//}
